﻿namespace StyleCop.CSharp
{
    /// <summary>
    /// A base class for all the StyleCop tests.
    /// </summary>
    /// <typeparam name="TRule">The type of the rule.</typeparam>
    public abstract class TestBase<TRule>
        where TRule : SourceAnalyzer
    {
        private readonly string ruleName;

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="TestBase&lt;TRule&gt;"/> class.
        /// </summary>
        /// <param name="ruleName">The identifier for the rule.</param>
        protected TestBase(string ruleName)
        {
            this.ruleName = ruleName;
        }
        #endregion Constructors

        #region Protected Methods
        /// <summary>
        /// Tests the specified source code.
        /// </summary>
        /// <param name="sourceCode">The source code.</param>
        /// <param name="expectedViolations">The expected violations.</param>
        protected void TestSource(string sourceCode, params ViolationInfo[] expectedViolations)
        {
            AnalysisHelper.Test(typeof(TRule), this.ruleName, sourceCode, expectedViolations);
        }

        protected void TestMethod(string methodCode, params ViolationInfo[] expectedViolations)
        {
            string sourceCode = "namespace MyNamespace { public class Class { " + methodCode + " } }";
            this.TestSource(sourceCode, expectedViolations);
        }
        #endregion Protected Methods
    }
}